/**
 * 船模 编辑页
 *
 * 作者 ganjun
 * 日期 2024-11-13 19:03:49
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#modelShip-edit'),
        laydate = layui.laydate,

        $sourceSelect = $view.find('#sourceSelect'),
        $shipPositionSelect = $view.find('#shipPositionSelect'),
        $shipTypeSelect = $view.find('#shipTypeSelect'),
        $modelTypeSelect = $view.find('#modelTypeSelect'),

        validate = layui.validate;

    initDictItemSelect();

    form.verify(validate);
    form.render();
    initFormValue();


    function initDictItemSelect() {
        if(modelShipData){
            //船模来源
            febs.initDictItemSelect($sourceSelect, 'SHIP_MODEL_SOURCE', modelShipData.source, null);
            //船模位置
            febs.initDictItemSelect($shipPositionSelect, 'SHIP_MODEL_POSITION', modelShipData.shipPosition, null);
            //船舶类型
            febs.initDictItemSelect($shipTypeSelect, 'SHIP_TYPE', modelShipData.shipType, null);
            //船模类型
            febs.initDictItemSelect($modelTypeSelect, 'SHIP_MODEL_TYPE', modelShipData.modelType, null);
        }else{
            //船模来源
            febs.initDictItemSelect($sourceSelect, 'SHIP_MODEL_SOURCE', null);
            //船模位置
            febs.initDictItemSelect($shipPositionSelect, 'SHIP_MODEL_POSITION', null);
            //船舶类型
            febs.initDictItemSelect($shipTypeSelect, 'SHIP_TYPE', null);
            //船模类型
            febs.initDictItemSelect($modelTypeSelect, 'SHIP_MODEL_TYPE', null);
        }
    }

    function initFormValue() {
        if (modelShipData) {
            form.val("modelShip-edit-form", {
                "id": modelShipData.id,
                "shipCode": modelShipData.shipCode,
                "projectCode": modelShipData.projectCode,
                "shipName": modelShipData.shipName,
                "source": modelShipData.source,
                "status": modelShipData.status,
                "demandEnddate": modelShipData.demandEnddate,
                "actuallyStartdate": modelShipData.actuallyStartdate,
                "actuallyEnddate": modelShipData.actuallyEnddate,
                "shipType": modelShipData.shipType,
                "rate": modelShipData.rate,
                "modelType": modelShipData.modelType,
                "applyDate": modelShipData.applyDate,
                "lengthOrigal": modelShipData.lengthOrigal,
                "lengthModel": modelShipData.lengthModel,
                "columnWidthOrigal": modelShipData.columnWidthOrigal,
                "columnWidthModel": modelShipData.columnWidthModel,
                "widthOrigal": modelShipData.widthOrigal,
                "widthModel": modelShipData.widthModel,
                "waterDeepOrigal": modelShipData.waterDeepOrigal,
                "waterDeepModel": modelShipData.waterDeepModel,
                "bodyDeepOrigal": modelShipData.bodyDeepOrigal,
                "bodyDeepModel": modelShipData.bodyDeepModel,
                "axieHightOrigal": modelShipData.axieHightOrigal,
                "axieHightModel": modelShipData.axieHightModel,
                "axieWidthOrigal": modelShipData.axieWidthOrigal,
                "axieWidthModel": modelShipData.axieWidthModel,
                "rectRate": modelShipData.rectRate,
                "blockNumber": modelShipData.blockNumber,
                "weight": modelShipData.weight,
                "headHeight": modelShipData.headHeight,
                "tailHeight": modelShipData.tailHeight,
                "headPush": modelShipData.headPush,
                "tailPush": modelShipData.tailPush,
                "memo": modelShipData.memo,
                "origShipCode": modelShipData.origShipCode,
            });
        }

        if(bmNo){
            form.val("modelShip-edit-form", {
                "shipCode": bmNo,
            });
        }

        if(now){
            form.val("modelShip-edit-form", {
                "applyDate": now,
            });
        }

        //编辑页面根据不同的source显示不同属性
        if(modelShipData && modelShipData.source){
            changeSource(modelShipData.source.toString());
        }
    }

    form.on('submit(modelShip-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    //船模来源选择控制
    form.on('select(sourceSelect)', function (data) {
        var val = data.value;
        changeSource(val);
    });

    //选择不通来源，显示不同属性
    function changeSource(srouceVal){
        if(srouceVal === '1'){
            $('#layui-row-source1').show();
            $('#layui-row-source2').show();
            $('#ship_major_element_fieldset').show();
            $('#ship_process_demand_fieldset').show();
            $('#origShipCodeDiv').hide();
        }else if(srouceVal === '2'){
            $('#layui-row-source1').hide();
            $('#layui-row-source2').hide();
            $('#ship_major_element_fieldset').hide();
            $('#ship_process_demand_fieldset').hide();
            $('#origShipCodeDiv').show();
        }else if(srouceVal === '3'){
            $('#layui-row-source1').hide();
            $('#layui-row-source2').hide();
            $('#ship_major_element_fieldset').hide();
            $('#ship_process_demand_fieldset').hide();
            $('#origShipCodeDiv').hide();
        }
    }

    //船模类型控制
    form.on('select(modelTypeSelect)', function (data) {
        var val = data.value;
        if(val === '5'){
            $('#modelTypeOtherDiv').show();
        }else{
            $('#modelTypeOtherDiv').hide();
        }
    });

    $('#lengthOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "lengthModel": modelVal,
            });
        }
    });

    $('#columnWidthOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "columnWidthModel": modelVal,
            });
        }
    });

    $('#widthOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "widthModel": modelVal,
            });
        }
    });

    $('#waterDeepOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "waterDeepModel": modelVal,
            });
        }
    });

    $('#bodyDeepOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "bodyDeepModel": modelVal,
            });
        }
    });


    $('#axieHightOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "axieHightModel": modelVal,
            });
        }
    });

    $('#axieWidthOrigal').bind('input propertychange', function(){
        var val = $(this).val();
        var rate = $('#rate').val();
        if(rate === ''|| rate === null){
            febs.alert.warn('缩尺比不能为空，请填写缩尺比！');

        }else{
            var modelVal = (val/rate).toFixed(4);
            form.val("modelShip-edit-form", {
                "axieWidthModel": modelVal,
            });
        }
    });



    function addOrUpdate(data) {
        if(data.source === '1'){
            if(data.rate === null || data.rate === ''){
                febs.alert.warn('新加工船模缩尺比不能为空');
                return;
            }
            //加工要素
            if(data.lengthOrigal === null || data.lengthOrigal === ''){
                febs.alert.warn('新加工船模总长（米）不能为空');
                return;
            }
            if(data.columnWidthOrigal === null || data.columnWidthOrigal === ''){
                febs.alert.warn('新加工船模两柱间长（米）不能为空');
                return;
            }
            if(data.widthOrigal === null || data.widthOrigal === ''){
                febs.alert.warn('新加工船模型宽（米）不能为空');
                return;
            }
            if(data.waterDeepOrigal === null || data.waterDeepOrigal === ''){
                febs.alert.warn('新加工船模设计吃水（米）不能为空');
                return;
            }
            if(data.bodyDeepOrigal === null || data.bodyDeepOrigal === ''){
                febs.alert.warn('新加工船模型深（米）不能为空');
                return;
            }
            if(data.axieHightOrigal === null || data.axieHightOrigal === ''){
                febs.alert.warn('新加工船模轴高（米）不能为空');
                return;
            }
            if(data.axieWidthOrigal === null || data.axieWidthOrigal === ''){
                febs.alert.warn('新加工船模轴间距（米）不能为空');
                return;
            }
            if(data.rectRate === null || data.rectRate === ''){
                febs.alert.warn('新加工船模方形系数不能为空');
                return;
            }
            //加工要求
            if(data.modelType == null || data.modelType == ''){
                febs.alert.warn('新加工船模类型不能为空');
                return;
            }
            if(data.blockNumber == null || data.blockNumber == ''){
                febs.alert.warn('新加工船模板块数不能为空');
                return;
            }
            if(data.weight == null || data.weight == ''){
                febs.alert.warn('新加工船模重量要求不能为空');
                return;
            }
            if(data.headHeight == null || data.headHeight == ''){
                febs.alert.warn('新加工船模首部升高不能为空');
                return;
            }
            if(data.headPush == null || data.headPush == ''){
                febs.alert.warn('新加工船模首侧推不能为空');
                return;
            }
            if(data.tailPush == null || data.tailPush == ''){
                febs.alert.warn('新加工船模尾侧推不能为空');
                return;
            }
        }else if(data.source === '2'){
            if(data.origShipCode == null || data.origShipCode == ''){
                febs.alert.warn('已有船模原船模编号不能为空');
                return;
            }
        }
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            data.sourceName = $view.find("select[name='source']").find("option:selected").text();
            data.shipTypeName = $view.find("select[name='shipType']").find("option:selected").text();
            data.modelTypeName = $view.find("select[name='modelType']").find("option:selected").text();
            data.shipPositionName = $view.find("select[name='shipPosition']").find("option:selected").text();
            data.projectName = $view.find("select[name='projectCode']").find("option:selected").text();

            if (data.id && data.id.length > 0) {
                febs.postArray(ctx + 'modelShip/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    // $('#febs-modelShip').find('#query').trigger('click');
                    var currentUrl = window.location.hash.replace('#', '');
                    febs.view.tab.del(currentUrl);
                    // febs.view.tab.change("/modelShip");
                    $('#febs-modelShip').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'modelShip', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    // $('#febs-modelShip').find('#query').trigger('click');
                    //改成跳转，提交之后的动作
                    var currentUrl = window.location.hash.replace('#', '');
                    console.log('======currentUrl=', currentUrl);
                    febs.view.tab.del(currentUrl);
                    // febs.view.tab.change("/modelShip");
                    // window.location.reload();
                    $('#febs-modelShip').find('#query').trigger('click');
                });
            }
        })
    }

    laydate.render({
        elem: "#applyDate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    laydate.render({
        elem: "#demandEnddate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    $('#reset').click(function () {
        closeCur();
    });

    function closeCur(){
        var currentUrl = window.location.hash.replace('#', '');
        febs.view.tab.del(currentUrl);
        // febs.view.tab.change("/modelShip");
        $('#febs-modelShip').find('#query').trigger('click');
    }
});